package net.m510.autosurfer.smbc;

import java.net.URI;
import java.util.List;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentPostProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;
import net.m510.autosurfer.core.Parameters;
import net.m510.autosurfer.core.utils.AnalyzeUtils;

import org.apache.http.NameValuePair;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.FormElement;
import org.jsoup.select.Elements;

/**
 * @author mgoto
 *
 */
public class SmbcLogin extends DocumentPostProcess {
	private static final Logger log = Logger.getLogger(SmbcLogin.class.getName());

	private String userId1;
	private String userId2;
	private String password;
	
	public SmbcLogin() {
		this(null,null);
	}
	public SmbcLogin(CloseableHttpClient client, HttpClientContext context) {
		super(client, context);
		super.log = log;
	}
	@Override
	protected List<NameValuePair> getRequestParameters(Document doc) {
		FormElement form = doc.getAllElements().forms().get(0);
		Parameters p = Parameters.create(form);
		p.put("USRID1", this.userId1);
		p.put("USRID2", this.userId2);
		p.put("USRID", userId1+userId2);
		p.put("PASSWORD", this.password);
		p.put("LOGIN_TYPE", "0");
		return p.asNameValueList();
	}

	@Override
	protected URI getUri(Document doc) {
		Elements elems = AnalyzeUtils.getFormDataElements(doc.getAllElements().forms().get(0)).select("[name=domainSumitomo]").select("[value]");
		if ( elems.isEmpty() ) throw new InvalidDocumentException("no next url",doc);
		return URI.create(elems.attr("value"));
	}
	public String getUserId1() {
		return userId1;
	}
	public void setUserId1(String userId1) {
		this.userId1 = userId1;
	}
	public String getUserId2() {
		return userId2;
	}
	public void setUserId2(String userId2) {
		this.userId2 = userId2;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}